package com.develop.BusinessGatewayService;

import java.util.concurrent.TimeUnit;

import com.develop.protocol.AlarmDecoder;
import com.develop.protocol.AlarmEncoder;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;

public class ClientChannelInitializer extends ChannelInitializer<SocketChannel> {
	private String host;
	private int port;
	private String cityCode;
	private String firmCode;
	private String omcType;

	public ClientChannelInitializer(String host, int port, String cityCode, String firmCode, String omcType) {
		this.host = host;
		this.port = port;
		this.cityCode = cityCode;
		this.firmCode = firmCode;
		this.omcType = omcType;
	}

	@Override
	protected void initChannel(SocketChannel socketChannel) throws Exception {
		ChannelPipeline pipeline = socketChannel.pipeline();
		// 字符串解码和编码
		pipeline.addLast(new ChannelHandler[] { new AlarmDecoder(32767, 7, 2, 0, 0, false) });
		pipeline.addLast(new ChannelHandler[] { new AlarmEncoder() });
		// 心跳检测
		pipeline.addLast(new IdleStateHandler(0, 60, 0, TimeUnit.SECONDS));
		// 客户端的逻辑
		pipeline.addLast("handler", new BusinessGatewayClientHandler(host, port, cityCode, firmCode, omcType));
	}
}